package com.whq.thrift.api.web.admin.db.methodinfo;

import java.util.Map;

import org.apache.ibatis.jdbc.SqlBuilder;

import com.whq.common.StringUtil;

public class MethodInfoSqlProvider {

	public String getByNameMethodsSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_method_info");

		if ((parameter.get("methodName") != null) && (StringUtil.isNotBlank(parameter.get("methodName").toString()))) {
			SqlBuilder.WHERE("method_name like #{methodName}");
		}

		if (parameter.get("methodStatus") != null) {
			SqlBuilder.WHERE("method_status = #{methodStatus}");
		}

		if (parameter.get("introDeveloper") != null && (StringUtil.isNotBlank(parameter.get("introDeveloper").toString()))) {
			SqlBuilder.WHERE("intro_developer like #{introDeveloper}");
		}

		SqlBuilder.ORDER_BY("id desc");
		return SqlBuilder.SQL() + " limit #{beginIndex}, #{maxCount} ";
	}

	public String getSelectCountSql(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("count(*)");
		SqlBuilder.FROM("t_method_info");

		if ((parameter.get("methodName") != null) && (StringUtil.isNotBlank(parameter.get("methodName").toString()))) {
			SqlBuilder.WHERE("method_name like #{methodName}");
		}

		if (parameter.get("methodStatus") != null) {
			SqlBuilder.WHERE("method_status = #{methodStatus}");
		}

		if (parameter.get("introDeveloper") != null && (StringUtil.isNotBlank(parameter.get("introDeveloper").toString()))) {
			SqlBuilder.WHERE("intro_developer like #{introDeveloper}");
		}

		return SqlBuilder.SQL();
	}

	public String getUpdateSql(MethodInfo methodInfo) {
		SqlBuilder.BEGIN();
		SqlBuilder.UPDATE("t_method_info");

		if (StringUtil.isNotBlank(methodInfo.getMethodName())) {
			SqlBuilder.SET("method_name = #{methodName}");
		}

		if (methodInfo.getUpdateTime() != null) {
			SqlBuilder.SET("update_time = #{updateTime}");
		}

		SqlBuilder.SET("is_2b = #{isTob}");
		SqlBuilder.SET("need_verify_ticket = #{needVerifyTicket}");
		SqlBuilder.SET("verify_module_id = #{verifyModuleId}");
		SqlBuilder.SET("signature_ignore_params = #{signatureIgnoreParams}");

		if (methodInfo.getMethodStatus() + "" != null) {
			SqlBuilder.SET("method_status = #{methodStatus}");
		}

		SqlBuilder.SET("intro_func = #{introFunc}");
		SqlBuilder.SET("intro_request_params = #{introRequestParams}");
		SqlBuilder.SET("intro_response = #{introResponse}");
		SqlBuilder.SET("intro_developer = #{introDeveloper}");
		SqlBuilder.SET("intro_comment = #{introComment}");

		if (methodInfo.getCreateTime() != null) {
			SqlBuilder.SET("create_time = #{createTime}");
		}

		SqlBuilder.SET("verify_module_method=#{verifyModuleMethod}");

		if (methodInfo.getTimeoutMs() != null) {
			SqlBuilder.SET("timeout_ms=#{timeoutMs}");
		}

		if (methodInfo.getMaxRunCountPreSecond() != null) {
			SqlBuilder.SET("max_run_count_pre_second=#{maxRunCountPreSecond}");
		}

		if (StringUtil.isNotBlank(methodInfo.getMethodModuleId())) {
			SqlBuilder.SET("method_module_id=#{methodModuleId}");
		}

		SqlBuilder.WHERE("id=#{id}");
		return SqlBuilder.SQL();
	}

	public String findApiInfoList(Map<String, Object> parameter) {
		SqlBuilder.BEGIN();
		SqlBuilder.SELECT("*");
		SqlBuilder.FROM("t_method_info");

		if ((parameter.get("introFunc") != null) && (StringUtil.isNotBlank(parameter.get("introFunc").toString()))) {
			SqlBuilder.WHERE("intro_func like #{introFunc}");
		}
		if ((parameter.get("methodName") != null) && (StringUtil.isNotBlank(parameter.get("methodName").toString()))) {
			SqlBuilder.WHERE("method_name like #{methodName}");
		}

		SqlBuilder.ORDER_BY("id desc");
		return SqlBuilder.SQL();
	}
}
